//用户 API 服务
import '../model/api_response.dart';
import '../model/user.dart';
import '../util/http_client.dart';
import '../util/parser_utils.dart';

class UserApi {
  static Future<ApiResponse<List<User>>> getUers() async {
    try {
      final response = await HttpClient.get("/users");
      return ParserUtils.parseObjectResponse<List<User>>(response,
          parser: (json) => json
              .map((e) => User.fromJson(e as Map<String, dynamic>))
              .toList() as List<User>);
    } catch (e) {
      return ApiResponse(
          data: null, status: 500, message: "Failed to get users: $e");
    }
  }

  static Future<ApiResponse<User>> createUser(User user) async{
    try{
      final userData = user.toJson()..remove('id');
      final response = await HttpClient.post("/users",body: userData);

      return ParserUtils.parseObjectResponse<User>(response, parser: (json)=>User.fromJson(json));

    }catch(e){
      return ApiResponse(data: null, status: 500, message:  "Failed to create user: $e",);
    }
  }
}
